package spring;

import spring.service.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

/**
 * @Description
 * @Author tuyou_cd
 * @Date 2020/6/19 16:03
 */
@Configuration
public class SpringRMIClientApp {

    @Profile("client")
    @Bean
    public RmiProxyFactoryBean rmiProxyFactoryBean() {
        RmiProxyFactoryBean factoryBean = new RmiProxyFactoryBean();
        //不能用大写RMI，否则要报错
        factoryBean.setServiceUrl("rmi://127.0.0.1:9999/myService");
        factoryBean.setServiceInterface(MyService.class);
        return factoryBean;
    }

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.getEnvironment().addActiveProfile("client");
        applicationContext.scan("me.tuyou");
        applicationContext.refresh();

        MyService bean = applicationContext.getBean(MyService.class);
        System.out.println(bean.add(11, 23));
        System.out.println(bean.sub(100, 99));
    }
}
